<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :style="defaultHeight"
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--人员数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label prop="inductionEmployee.employeeCode" label-width="40px">
            <el-input
              v-model="queryParams.inductionEmployee.employeeCode"
              placeholder="请输入警号"
              clearable
              style="width: 120px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label prop="inductionEmployee.employeeName" label-width="40px">
            <el-input
              v-model="queryParams.inductionEmployee.employeeName"
              placeholder="请输入名称"
              clearable
              style="width: 120px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label prop="inductionEmployee.employeeIdCard" label-width="40px">
            <el-input
              v-model="queryParams.inductionEmployee.employeeIdCard"
              placeholder="请输入身份证号"
              clearable
              style="width: 180px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label prop="inductionEmployee.employeeType" label-width="40px">
            <el-select v-model="queryParams.inductionEmployee.employeeType" placeholder="辅警类型" clearable style="width: 108px">
              <el-option v-for="dict in dict.type.emp_employee_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label prop="inductionEmployee.employeeLevel" label-width="40px">
            <el-select v-model="queryParams.inductionEmployee.employeeLevel" placeholder="辅警层级" clearable style="width: 108px">
              <el-option v-for="dict in dict.type.emp_employee_level" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label prop="inductionEmployee.employeeAgency" label-width="40px">
            <el-select v-model="queryParams.inductionEmployee.employeeAgency" placeholder="机构层级" clearable style="width: 108px">
              <el-option v-for="dict in dict.type.emp_employee_agency" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label prop="status" label-width="40px">
            <el-select v-model="queryParams.status" placeholder="审核状态" clearable style="width: 108px">
              <el-option v-for="dict in dict.type.emp_audit_status" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              :disabled="single || disable2 || disable3"
              @click="handleAdd"
              v-hasPermi="['employee:induction:add']"
            >入职</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single || disable2 || disable3"
              @click="handleUpdate"
              v-hasPermi="['employee:induction:add']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-upload2"
              size="mini"
              :disabled="multiple || disable2 || disable3"
              @click="handleSubmit"
              v-hasPermi="['employee:induction:submit']"
            >提交</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-download"
              size="mini"
              :disabled="multiple || disable1 || disable4 || disable3"
              @click="handleReject"
              v-hasPermi="['employee:induction:reject']"
            >驳回</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-check"
              size="mini"
              :disabled="multiple || !disable2 || disable1 || disable4 || disable3"
              @click="handleAudit"
              v-hasPermi="['employee:induction:audit']"
            >审核</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange" stripe>
          <el-table-column type="selection" width="42" />
          <el-table-column label="警号" align="center" prop="inductionEmployee.employeeCode" width="67" />
          <el-table-column label="名称" align="center" prop="inductionEmployee.employeeName" width="76" />
          <el-table-column label="身份证号" align="center" prop="inductionEmployee.employeeIdCard" width="162" />
          <el-table-column label="联系电话" align="center" prop="inductionEmployee.employeePhone" width="106" />
          <el-table-column label="入职日期" align="center" prop="inductionEmployee.employeeInductionDate" width="92" />
          <el-table-column label="审核状态" align="center" prop="status" width="72">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.emp_audit_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="类型" align="center" prop="inductionEmployee.employeeType" width="48">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.emp_employee_type" :value="scope.row.inductionEmployee.employeeType" />
            </template>
          </el-table-column>
          <el-table-column label="层级" align="center" prop="inductionEmployee.employeeLevel" width="48">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.emp_employee_level" :value="scope.row.inductionEmployee.employeeLevel" />
            </template>
          </el-table-column>
          <el-table-column label="机构" align="center" prop="inductionEmployee.employeeAgency" width="72">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.emp_employee_agency" :value="scope.row.inductionEmployee.employeeAgency" />
            </template>
          </el-table-column>
          <el-table-column label="招聘类型" align="center" prop="inductionEmployee.employeeRecruitType" width="72">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.emp_recruit_type" :value="scope.row.inductionEmployee.employeeRecruitType" />
            </template>
          </el-table-column>
          <el-table-column label="招聘批次" align="center" prop="inductionEmployee.employeeRecruitBatch" width="72">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.emp_recruit_batch" :value="scope.row.inductionEmployee.employeeRecruitBatch" />
            </template>
          </el-table-column>
          <el-table-column label="招聘日期" align="center" prop="inductionEmployee.employeeRecruitDate" width="92" />
          <el-table-column label="部门" align="center" prop="inductionEmployee.employeeDepartment.deptName" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" width="108" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-date" @click="handleDetails(scope.row)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <el-drawer :visible.sync="drawer" :with-header="false" size="313px">
      <el-row style="padding:4px 16px;font-weight:500;font-size::14px;font-weight: bold;">{{form.employeeName}}</el-row>
      <el-row style="padding-left:16px;">
        <el-tag type="info" class="drawerTag">
          <i class="el-icon-coordinate" v-if="form.employeeCode">{{form.employeeCode}}</i>
          <i class="el-icon-coordinate" v-else>警号</i>
        </el-tag>
        <el-tag type="primary" class="drawerTag">
          <i class="el-icon-suitcase-1">{{form.status == 0 ? "待职":(form.status == 1? "在职":"离职")}}</i>
        </el-tag>
        <el-tag type="info" class="drawerTag">
          <i class="el-icon-bangzhu" v-if="form.employeeType">{{form.employeeType == 0 ? "文职":"勤务"}}</i>
          <!-- <i class="el-icon-bangzhu" v-else>辅警类型</i> -->
        </el-tag>
        <el-tag type="info" class="drawerTag">
          <i class="el-icon-date" v-if="form.employeeInductionDate">{{form.employeeInductionDate}}</i>
          <i class="el-icon-date" v-else>入职日期</i>
        </el-tag>
      </el-row>
      <el-tabs v-model="activeName" style="margin: 0px 18px;">
        <el-tab-pane label="基本信息" name="1">
          <el-descriptions direction="vertical" :column="2" border style="padding:0px;" size="small" class="scrollable-descriptions">
            <!-- <el-descriptions-item label="名称">{{form.employeeName}}</el-descriptions-item> -->
            <el-descriptions-item label="联系电话">{{form.employeePhone}}</el-descriptions-item>
            <el-descriptions-item label="证件号码">{{form.employeeIdCard}}</el-descriptions-item>
            <!-- <el-descriptions-item label="证件号码" :span="2">{{form.employeeIdCard}}</el-descriptions-item> -->
            <el-descriptions-item label="归属部门" :span="2">{{form.employeeDepartment != null ? form.employeeDepartment.deptName : "--"}}</el-descriptions-item>
            <el-descriptions-item label="机构层级">
              <dict-tag :options="dict.type.emp_employee_agency" :value="form.employeeAgency" />
            </el-descriptions-item>
            <el-descriptions-item label="辅警层级">
              <dict-tag :options="dict.type.emp_employee_level" :value="form.employeeLevel" />
            </el-descriptions-item>
            <el-descriptions-item label="性别">
              <el-tag size="small">
                <dict-tag :options="dict.type.sys_user_sex" :value="form.employeeSex" />
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="学历">
              <dict-tag :options="dict.type.emp_employee_education" :value="form.employeeEducation" />
            </el-descriptions-item>
            <el-descriptions-item label="年龄">{{form.employeeAge}}</el-descriptions-item>
            <el-descriptions-item label="出生年月">{{form.employeeBirthday}}</el-descriptions-item>
            <el-descriptions-item label="政治面貌">
              <dict-tag :options="dict.type.emp_employee_politics" :value="form.employeePolitics" />
            </el-descriptions-item>
            <el-descriptions-item label="户籍地址">
              <dict-tag :options="dict.type.emp_employee_domicile" :value="form.employeeDomicile" />
            </el-descriptions-item>
            <el-descriptions-item label="招聘类型">
              <dict-tag :options="dict.type.emp_recruit_type" :value="form.employeeRecruitType" />
            </el-descriptions-item>
            <el-descriptions-item label="招聘批次">
              <dict-tag :options="dict.type.emp_recruit_batch" :value="form.employeeRecruitBatch" />
            </el-descriptions-item>
            <el-descriptions-item label="招聘日期" :span="2">
              <el-tag size="small">{{form.employeeRecruitDate}}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="紧急联系人">{{form.employeeEmergency}}</el-descriptions-item>
            <el-descriptions-item label="紧急联系电话">{{form.employeeEmergencyPhone}}</el-descriptions-item>
            <el-descriptions-item label="转正日期">{{form.employeeConfirmationDate}}</el-descriptions-item>
            <el-descriptions-item label="离职日期">{{form.employeeLeaveDate}}</el-descriptions-item>
            <el-descriptions-item label="合同编码"></el-descriptions-item>
            <el-descriptions-item label="档案编码">{{form.employeeArchives}}</el-descriptions-item>
            <el-descriptions-item label="备注" :span="2">{{form.remark}}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </div>
</template>

<script>
import { deptTreeSelect } from '@/api/system/user';
import { listInduction, submitInduction, rejectInduction, auditInduction } from '@/api/employee/induction';
import { getEmployee } from '@/api/employee/list';

export default {
  name: 'Employee',
  dicts: [
    'sys_user_sex',
    'emp_employee_status',
    'emp_employee_level',
    'emp_employee_agency',
    'emp_employee_education',
    'emp_employee_politics',
    'emp_employee_domicile',
    'emp_employee_type',
    'emp_audit_status',
    'emp_recruit_type',
    'emp_recruit_batch'
  ],
  data() {
    return {
      form: {},
      // 遮罩层
      loading: true,
      // 选中数组
      employeeIds: [],
      // 选中数组
      employeeCodes: [],
      // 选中数组
      employeeInductionDates: [],
      // 选中数组
      inductionIds: [],
      // 选中数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      employeeList: null,
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined,
        inductionEmployee: {
          employeeCode: undefined,
          employeeName: undefined,
          employeeIdCard: undefined,
          employeeType: undefined,
          employeeLevel: undefined,
          employeeAgency: undefined,
          deptId: undefined
        }
      },
      // 抽屉
      drawer: false,
      // Tab默认
      activeName: '1',
      // 待审核禁用
      disable1: false,
      // 审核中禁用
      disable2: false,
      // 已审核禁用
      disable3: false,
      // 未通过禁用
      disable4: false,
      // 部门树状结构高度
      defaultHeight: {
        height: ''
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getHeight();
  },
  methods: {
    /** 查询人员列表 */
    getList() {
      this.loading = true;
      listInduction(this.queryParams, this.queryParams.pageNum, this.queryParams.pageSize).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.inductionEmployee.deptId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.queryParams.inductionEmployee.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.inductionIds = selection.map(item => item.inductionId);
      this.employeeIds = selection.map(item => item.employeeId);
      this.employeeCodes = selection.map(item => item.inductionEmployee.employeeCode);
      this.employeeInductionDates = selection.map(item => item.inductionEmployee.employeeInductionDate);
      this.names = selection.map(item => item.inductionEmployee.employeeName);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.disable2 = selection.map(item => item.status).includes('1');
      this.disable1 = selection.map(item => item.status).includes('0');
      this.disable4 = selection.map(item => item.status).includes('3');
      this.disable3 = selection.map(item => item.status).includes('2');
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      const inductionId = row.inductionId || this.inductionIds;
      this.$router.push('/employee/employee-induction/add/' + inductionId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const inductionId = row.inductionId || this.inductionIds;
      this.$router.push('/employee/employee-induction/add/' + inductionId);
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      this.drawer = true;
      getEmployee(row.employeeId).then(response => {
        this.form = response.data;
      });
    },
    /** 提交按钮操作 */
    handleSubmit() {
      const inductionIds = this.inductionIds;
      const employeeNames = this.names;
      if (this.employeeCodes.some(code => code === null)) {
        this.$modal.msgError('您所选中的提交数据中，存在警号为空的数据，请填写警号后进行再次提交！');
        return;
      }
      if (this.employeeInductionDates.some(date => date === null)) {
        this.$modal.msgError('您所选中的提交数据中，存在入职日期为空的数据，请填写入职日期后进行再次提交！');
        return;
      }
      this.$modal
        .confirm('提交后数据不可再修改，是否确认提交名称为"' + employeeNames + '"的数据项？')
        .then(function () {
          return submitInduction(inductionIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('提交成功');
        })
        .catch(() => {});
    },
    /** 驳回按钮操作 */
    handleReject() {
      const inductionIds = this.inductionIds;
      const employeeNames = this.names;
      this.$modal
        .confirm('是否确认驳回名称为"' + employeeNames + '"的数据项？')
        .then(function () {
          return rejectInduction(inductionIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('驳回成功');
        })
        .catch(() => {});
    },
    /** 审核按钮操作 */
    handleAudit() {
      const inductionIds = this.inductionIds;
      const employeeIds = this.employeeIds;
      const employeeNames = this.names;
      this.$modal
        .confirm('审核后数据不可再修改，是否确认审核名称为"' + employeeNames + '"的数据项？')
        .then(function () {
          return auditInduction(inductionIds, employeeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('审核成功');
        })
        .catch(() => {});
    },
    // 计算部门树状结构高度
    getHeight() {
      this.defaultHeight.height = window.innerHeight - 145 + 'px';
    }
  }
};
</script>
<style scoped>
.drawerTag {
  padding: 0px;
  line-height: 15px;
  height: 15px;
  margin-right: 4px;
}
.el-tooltip__popper {
  max-width: 20%;
}
.el-tree {
  /* display: flex; */
  overflow: hidden;
}
.el-tree:hover {
  overflow: auto;
}
</style>